package com.mapforge.gui.frames.tilelayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;

import com.mapforge.collection.WeakReferenceInstanceProvider;
import com.mapforge.listener.TileLayerCreationListener;
import com.mapforge.listener.TileMapCreationEvent;
import com.mapforge.listener.TileMapCreationListener;
import com.mapforge.map.TileMap;

public class NewTileLayerDialogProvider extends
		WeakReferenceInstanceProvider<NewTileLayerDialog> implements
		TileMapCreationListener, ActionListener {

	private TileMap tileMap;
	private JFrame parent;
	private List<TileLayerCreationListener> tileLayerCreationListeners;

	public NewTileLayerDialogProvider(JFrame parent) {
		this.parent = parent;
		tileLayerCreationListeners = new ArrayList<TileLayerCreationListener>();
	}

	public void addTileLayerCreationListener(TileLayerCreationListener listener) {
		tileLayerCreationListeners.add(listener);
	}

	public void clearTileLayerCreationListeners() {
		tileLayerCreationListeners.clear();
	}

	public boolean removeTileLayerCreationListener(
			TileLayerCreationListener listener) {
		return tileLayerCreationListeners.remove(listener);
	}

	public Iterator<TileLayerCreationListener> tileLayerCreationListenersIterator() {
		return tileLayerCreationListeners.iterator();
	}

	/**
	 * Returns a new instance of {@link NewTileLayerDialogProvider}
	 */
	protected NewTileLayerDialog createNewInstance() {
		return new NewTileLayerDialog(parent, tileLayerCreationListeners,
				tileMap);
	}

	public void tileMapCreationPerformed(TileMapCreationEvent mapCreationEvent) {
		this.tileMap = mapCreationEvent.getTileMapCreated();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.getInstance().setVisible(true);
	}
}